package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.controller.PageResult;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;

    // 分页查询
    @Override
    public PageResult page(Integer page, Integer pageSize, String name, LocalDate begin, LocalDate end) {
        // 开启分页
        PageHelper.startPage(page, pageSize);
        // 调用 mapper 分页查询
        Page<Clazz> clazzPage = clazzMapper.page(name, begin, end);
        // 封装 PageResult 并返回
        return new PageResult(clazzPage.getTotal(), clazzPage.getResult());
    }


    //删除班级
    @Override
    public Boolean deleteById(Integer id) {
        Boolean falg=clazzMapper.deleteById(id);
        if(falg){
            return true;
        }
        return false;
    }

    //添加班级
    @Override
    public void save(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.insert(clazz);
    }
    //根据id回显班级信息
    @Override
    public Clazz getById(Integer id) {
        return clazzMapper.getById(id);
    }
    @Transactional
    @Override
    public void update(Clazz clazz) {
    clazzMapper.updateById(clazz);
    }

    @Override
    public List<Clazz> list() {
       List<Clazz> list = clazzMapper.list();
       return list;
    }
}